package com.next

class Project
{
    String name

    String description

    Date createdDate = new Date()
    Date modifiedDate = new Date()
    User createBy
    User modifyBy

    User user

    static hasMany = [users: ProjectTeam]

    static constraints = {
        name maxSize: 64, unique: true
        description blank: true, nullable: true, type: "text"

        user nullable: true, blank: true
        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)

        if (!user)
        {
            user = createBy
        }

        ProjectTeam projectTeam = new ProjectTeam(project: this, user: user)
        this.addToUsers(projectTeam)
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }

    String toString()
    {
        name
    }
}
